home *** CD-ROM | disk | FTP | other *** search
/ Compendium Deluxe 1 / LSD Compendium Deluxe 1.iso / a / programming / assembly / kreatorvec.lha / storage / vectors / VECTOR.TXT < prev    next >
Encoding:
Text File  |  1990-09-06  |  4.8 KB  |  171 lines

  1.       ---------------- More advanced coding techniques ----------------
  2.                ----------- By Kreator of ANARCHY UK ----------
  3.                             ---- 3 D routines ----
  4.  
  5.   I am  going to  approach this  topic
  6. from a mathematical point of view, the
  7. mathematics invloved  are quite simple
  8. but if you  are not particularly adept
  9. in  this  area  dont  worry  it  isn't
  10. essential to understand the underlying
  11. theory.
  12.   Now  suppose we  are given an object
  13. to    transfer   into    a   wireframe
  14. representation  on  screen.   We  must
  15. construct a  list of coordinates which
  16. specify the vertices of the object and
  17. also a connection list which tells the
  18. computer how to  connect these points
  19. together.
  20.  eg.    A cube  has 8  vertices,  and
  21. 12 connecting sides, the vertices are
  22. as follows
  23. (50,50,50)    (-50,50,50) (-50,-50,50)
  24. (50,-50,50)   (50,50,-50) (50,-50,-50)
  25. (-50,-50,-50) (-50,50,-50)
  26.  
  27. and if these are then labelled 1-8 we
  28. have the connections as follows
  29.  1-2  2-3  3-4  4-1 5-6  6-7  7-8  8-5
  30.  1-5  2-6  3-7  4-8
  31.  
  32.   If you don't believe the example try
  33. to   draw   the   cube   yourself  and
  34. visualise the coordinates. Notice that
  35. the point  (0,0,0) is at the centre of
  36. the cube, this is important because in
  37. our  rotations this  will  be the only
  38. point which remains stationary.
  39.  
  40.   When rotating the object what we are
  41. in fact doing is rotating the vertices
  42. about the fixed ORIGIN (0,0,0).  There
  43. is a mathematical theorem which states
  44. that any 3 dimensional rotation can be
  45. split into  3 individual  rotations in
  46. only  2  dimensions,  which is  a much
  47. simpler  thing to  calculate.  Now  in
  48. general  it  is   quite  difficult  to
  49. calculate  these  rotations   from  an
  50. arbitrary  3D  rotation,  but  happily
  51. enough   this   doesn't   matter  when
  52. writing   demos   because   by  simply
  53. performing  2D  rotations and  varying
  54. the  3 Angles of  rotation we  achieve
  55. an interesting effect.
  56.  
  57.   The formula for 2D rotation is given
  58. as follows,
  59.  
  60.    x = X cos(s) - Y sin(s)
  61.    y = Y cos(s) + X sin(s)
  62.  
  63.   This can easily be shown with simple
  64. trigonometry.
  65.  
  66.   These formulae enable us to rotate a
  67. point in just two dimensions,  but all
  68. we now do is to rotate the point three
  69. times in different planes.
  70.   In  other words  if we  are given  a
  71. general  point  (x,y,z)  and a,b,c are
  72. the three  angles of  rotation then to
  73. calculate the rotated point follow the
  74. procedure below (just for interest the
  75. angles  a,b,c  are  called  the  Euler 
  76. angles)
  77.  
  78.    x1 = x cos(a) - y sin(a)
  79.    y1 = y cos(a) + x sin(a)
  80.  
  81.    y2 = y1 cos(b) -  z sin(b)
  82.    z1 = z  cos(b) + y1 sin(b)
  83.  
  84.    z2 = z1 cos(c) - x1 sin(c)
  85.    x2 = x1 cos(c) + z1 sin(c)
  86.  
  87.   Then  (x2,y2,z2)  holds  the rotated
  88. coordinate.  To implement  this on the
  89. Amiga use a  Sintable which has values
  90. from  -32768 to  32767,  this  can  be
  91. reused  for  the  cosine  calculations
  92. as  cos (a) = sin (a+90 degrees).  You
  93. could code the  routine something like
  94.  
  95.     Move x,d3
  96.     Move y,d4
  97.     Move z,d5
  98.     Lea  Sin,a0
  99.     Lea  Cos,a1
  100.     Move d3,d6
  101.     Move d4,d7
  102.     Move a,d0    ;a holds 2x the angle
  103.     Move (a0,d0),d1
  104.     Move (a1,d0),d2
  105.  
  106.     Muls d2,d6
  107.     Muls d1,d7
  108.     Sub  d7,d6
  109.     Add.l d6,d6
  110.     Swap d6      ;Calculation of x1
  111.  
  112.     Muls d2,d4
  113.     Muls d1,d3
  114.     Add  d3,d4
  115.     Add.l d4,d4
  116.     Swap d4      ;Calculation of y1
  117.   etc......
  118.  
  119. Now up until now we haven't considered
  120. how  the lines  will be  drawn to  the
  121. screen, I shall assume you have access
  122. to a blitter line draw routine.
  123. There are two options open now
  124. we can  leave the  coordinates as they
  125. are and  simply add a  displacement to
  126. them before  plotting the lines, or go
  127. for  the  more  realsitic technique of
  128. perspective. This invloves scaling the
  129. x,y coords.  according to how far into
  130. the screen we are. A reasonable way of
  131. doing that is as follows
  132.  
  133.      x,y,z  in d3,d4,d5
  134.  
  135.      Add    #Depth+Scale,d5
  136.      Move.l #Scale*65536,d6
  137.      Divu   d6,d5
  138.  
  139.      Muls   d5,d3 ;Scale the X coord
  140.      Add.l  d3,d3
  141.      Swap   d3
  142.  
  143.      Muls   d5,d4 ;Scale the Y coord
  144.      Add.l  d3,d4
  145.      Swap   d4
  146.  
  147.   Alternatively you can use a table of
  148. scaling values.
  149.   Now all that  remains is to plot the
  150. lines. Dont  forget up  until  now all
  151. vertices have been calculated with the
  152. origin at  (0,0)  but now we must move
  153. the origin to the centre of the screen
  154. or where ever  else you want it.  This
  155. means  adding a  displacement to  each
  156. pair of coordinates.
  157.  
  158.   These routines can easily be adapted
  159. to other purposes, eg.to create vector
  160. bobs,  use a single point for each bob
  161. and   before  plotting   sort   the  z
  162. coordinates  and  plot  the   bobs  in
  163. reverse order,  also a  simple form of
  164. hidden line removal can be implemented
  165. by   creating  a   list  of  surfaces,
  166. calculating   the  normals   to  these
  167. surfaces and if the normals point away
  168. from  you dont  plot any  lines in the
  169. surface. For an example of this see my
  170. Magnetic Fields Party Demo.
  171.